package com.mazaiting.redeye.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.MultipartConfigElement;

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-06-28
 * @decription 静态文件与动态文件上传路径配置
 * 静态资源访问: http://localhost:8088/image/xxx.jpg
 * 动态上传: /Users/mazaiting/Data/program/project/red_eye/red_eye_server/image
 */
@Configuration
public class FileConfig implements WebMvcConfigurer {
    /**
     * 静态访问路径
     */
    @Value("${file.staticAccessPath}")
    private String mStaticAccessPath;
    /**
     * 上传文件夹
     */
    @Value("${file.uploadFolder}")
    private String mUploadFolder;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 设置静态资源访问路径
        registry.addResourceHandler(mStaticAccessPath).addResourceLocations("file:///" + mUploadFolder);
    }

    /**
     * 多文件上传配置结点
     * @return MultipartConfigElement
     */
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setLocation(mUploadFolder);
        return factory.createMultipartConfig();
    }
}
